<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class baogong extends CI_Controller{
    private $appId;
    private $appSecret;
    
    function __construct() {
        parent::__construct();
        $this->appId = "wxd2b4ceb62757c00c";
        $this->appSecret = "rpLCiPzZ83mwIz4Ji6lXGTlaM6dmdjU9RwE44BXmvok";
        $this->load->library('baogong');
    }
    public function index() {
        $userid = '6522';
        $this->load->library('baogong');
        $jsapiTicket = $this->getJsApiTicket();
        
        // 注意 URL 一定要动态获取，不能 hardcode.
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        
        //$userInfo = $this->getSoapUserName($userid);
        /*
        if($userInfo[0] == "成功"){
            $status = "开通";
        }else{
            $status = "未开通";
        }var_dump("s");
         * 
         */
        $status = "开通";
        $EmpUserNo = "E6522";
        $EmpUserName = "张雪林";
        /*
        $EmpUserNo = "E".$userInfo['2'];
        $EmpUserName = $userInfo['3'];
         * 
         */
        $timestamp = time();
        $nonceStr = $this->createNonceStr();

        // 这里参数的顺序要按照 key 值 ASCII 码升序排序
        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
        //var_dump($jsapiTicket);
        $signature = sha1($string);

        $status = "开通";
        $signPackage = array(
            "appId"     => $this->appId,
            "nonceStr"  => $nonceStr,
            "timestamp" => $timestamp,
            "url"       => $url,
            "signature" => $signature,
            "rawString" => $string,
            "userid"    => $userid,
            "status"    => $status,
            "EmpUserNo" => $EmpUserNo,
            "EmpUserName" => $EmpUserName
        );
        var_dump($signPackage);
        $this->load->view("baogong",$signPackage);
    }

  private function createNonceStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
          $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
  }

  private function getJsApiTicket() {
        // jsapi_ticket 应该全局存储与更新，以下代码以写入到文件中做示例
        //$data = json_decode(file_get_contents("weixinJS/jsapi_ticket.json"));
        $data = $this->Data_model->get_single_data(array('type' => 'jsapi_ticket'),"WEIXININFOZJ");
        if ($data['time'] < time()) {
            $accessToken = $this->get_access_token();
            // 如果是企业号用以下 URL 获取 ticket
            $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
            //$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
            $res = json_decode($this->http_request_json($url));
            $ticket = $res->ticket;
            if ($ticket) {
                $this->Data_model->update_data(array('type' => 'jsapi_ticket'),array('time' => time() + 7000,'jsapi_ticket' => $ticket),"WEIXININFOZJ");
                /*
                        $data->expire_time = time() + 7000;
                $data->jsapi_ticket = $ticket;
                $fp = fopen("weixinJS/jsapi_ticket.json", "w");
                fwrite($fp, json_encode($data));
                fclose($fp);
                 * 
                 */
            }
        } else {
            $ticket = $data['jsapi_ticket'];
        }
        return $ticket;
  }

    public function Login(){
        if (isset($_GET['code'])){
            $code = $_GET['code'];
            $res = $this->Data_model->get_single_data(array('name'=>'中骏森驰',
                                                            'type'=>'Baogong_6'),"WEIXININFOZJ");
            $this->get_userId($res['access_token'], $code,$res['agentid']);
        }else{
            echo "NO CODE";
        }
    }
    
    function get_userId($access_token,$code,$agentId){
        if($access_token){
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=".$access_token."&code=".$code."&agentid=".$agentId;
            $json=self::http_request_json($url);//这个地方不能用file_get_contents
            $data=json_decode($json,true);
            if(isset($data['errcode'])){
                if($data['errcode'] == "40001" or $data['errcode'] == '42001' or $data['errcode'] == '40014'){
                    $this->get_access_token();
                    
                    $newurl = "<?=base_url()?>/baogong/Login?code=".$code;
                    echo "<script language='javascript' type='text/javascript'>";  
                    echo "window.location.href='".$newurl."'";  
                    echo "</script>"; 
                    
                }else{
                    echo "页面请求错误，请检查！";
                }
            }else{
                $this->index($data['UserId']);
            }
        }else{
            $this->get_access_token();
            $this->Login();
        }
    }
    function get_access_token(){
        $result = $this->Data_model->get_single_data(array('name'=>'中骏森驰',
                                                            'type'=>'Baogong_6'),"WEIXININFOZJ");
        if($result['time'] < time()){
            $corpid = $result['corpid'];
            $corpsecret = $result['corpsecret'];
            $url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?&corpid=".$corpid."&corpsecret=".$corpsecret;
            $json=self::http_request_json($url);//这个地方不能用file_get_contents
            $data=json_decode($json,true);
            if(isset($data['access_token'])){
                $this->Data_model->update_data(array('name'=>'中骏森驰',
                                                                'type'=>'Baogong_6'),array('access_token' => $data['access_token'],'time' => time()+7100),'WEIXININFOZJ');
            
                $access_token = $data['access_token'];
            }else{
                return "获取access_token错误";
            } 
        }else{
            $access_token = $result['access_token'];
        }
        return $access_token;
    }
    /*
    function get_access_token(){
        $result = $this->Data_model->get_single_data(array('name'=>'中骏森驰',
                                                            'type'=>'Check_47'),"WEIXININFOZJ");
        $corpid = $result['corpid'];
        $corpsecret = $result['corpsecret'];
        $url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?&corpid=".$corpid."&corpsecret=".$corpsecret;
        $json=self::http_request_json($url);//这个地方不能用file_get_contents
        $data=json_decode($json,true);
        if(isset($data['access_token'])){
            $this->Data_model->update_data(array('name'=>'中骏森驰',
                                                            'type'=>'Check_47'),array('access_token' => $data['access_token']),'WEIXININFOZJ');
        }else{
            return "获取access_token错误";
        }        
    }
     * 
     */
    //因为url是https 所有请求不能用file_get_contents,用curl请求json 数据
    function http_request_json($url){  
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;  
    }
    
    function downloadfile(){
        $mediaid = $_REQUEST['mediaid'];
        $this->Data_model->update_data(array('id' => '4'),array('jsapi_ticket' => $mediaid),'WEIXININFOZJ');
        $access_token = $this->get_access_token();
        $url = "https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=".$access_token."&media_id=".$mediaid;
        $fileInfo = $this->downloadWeixinFile($url);
	$savePath = "html/image/";
        $filename = $savePath.date("Ymd",  time())."_".$mediaid.".jpg";
        $this->saveWeixinFile($filename, $fileInfo["body"]);
        echo $filename;
    }
    function downloadWeixinFile($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);    
        curl_setopt($ch, CURLOPT_NOBODY, 0);    //只取body头
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $package = curl_exec($ch);
        $httpinfo = curl_getinfo($ch);
        curl_close($ch);
        $imageAll = array_merge(array('header' => $httpinfo), array('body' => $package)); 
        return $imageAll;
    }
 
    function saveWeixinFile($filename, $filecontent)
    {
        $local_file = fopen($filename, 'w');
        if (false !== $local_file){
            if (false !== fwrite($local_file, $filecontent)) {
                fclose($local_file);
            }
        }
    }
    public function getSoapUserName($userid){
        $userEmp = "E".$userid;
        header("content-type:text/html;charset=utf-8");
        try {
            $client = new SoapClient("http://192.168.0.41/BHService.asmx?wsdl");
            
            //1511137-4
            //一、完工汇报进入验证
            $parmas = array("strIP"       => "192.168.0.159",
                            "strModule"   => "MES",
                            "strUser"     => "192.168.0.159",
                            "strValue"    => $userEmp);
            $return = $client->CheckSubmitBarCode($parmas);
            //var_dump($return->CheckSubmitBarCodeResult->string);
            return $return->CheckSubmitBarCodeResult->string;
        } catch (SOAPFault $e) {
            print_r('Exception:'.$e);
        }
    }
     public function test(){
        header("content-type:text/html;charset=utf-8");
        try {
            $client = new SoapClient("http://192.168.0.41/BHService.asmx?wsdl");
            
            //1511137-4
            //一、完工汇报进入验证
            $parmas = array("strIP"       => "192.168.0.159",
                            "strModule"   => "MES",
                            "strUser"     => "192.168.0.159",
                            "strValue"    => "E6522");
            $return = $client->CheckSubmitBarCode($parmas);
            //var_dump($return->CheckSubmitBarCodeResult->string);
            //var_dump($return->CheckSubmitBarCodeResult->string); 
        } catch (SOAPFault $e) {
            print_r('Exception:'.$e);
        }
    }
    public function getSoapBarCodeType(){
        $value = trim($_REQUEST['value']);
        header("content-type:text/html;charset=utf-8");
        try {
            $client = new SoapClient("http://192.168.0.41/BHService.asmx?wsdl");
            
            //1511137-4
            //一、完工汇报进入验证
            $parmas = array("strIP"       => "192.168.0.159",
                            "strModule"   => "MES",
                            "strUser"     => "192.168.0.159",
                            "strValue"    => $value);
            $return = $client->CheckSubmitBarCode($parmas);
            
            $return = $return->CheckSubmitBarCodeResult->string;
            
            $jsonArr = array("res" => $return[0],
                                "type" => $return[1],
                                "msg1" => $return[2],
                                "msg2" => $return[3]);
            foreach ( $jsonArr as $key => $value ) { 
                $jsonArr[$key] = urlencode ( $value );  
            }
            echo urldecode(json_encode($jsonArr));
            //return $return->CheckSubmitBarCodeResult->string;
        } catch (SOAPFault $e) {
            print_r('Exception:'.$e);
        }
    }
    public function getSoap(){
        header("content-type:text/html;charset=utf-8");
        try {
            $client = new SoapClient("http://192.168.0.41/BHService.asmx?wsdl");
            /*
            //一、完工汇报进入验证
            $parmas = array("strIP"       => "192.168.0.1",
                            "strModule"   => "MES",
                            "strUser"     => "123",
                            "strValue"    => "1511137-4");
            $return = $client->CheckSubmitBarCode($parmas);
            var_dump($return->CheckSubmitBarCodeResult->string);
             * 
             */
            $return = array("FTypeNumber" => "EMP",     //
                            "FTypeName"   => "11",
                            "FValue" => "E95910",
                            "FText" => "22",);
            $parmas = array("strIP"      => "192.168.0.1",
                            "strModule"  => "MES",
                            "strUser"    => "11",
                            "strType"    => "WGHB",
                            "strEmp"     => "44",
                            "strLZK"     => "1511137-4",
                            "strChkJson"      => "[".json_encode($return)."]");
            $return = $client->GetTaskingByJson($parmas);
            //var_dump($return);
        } catch (SOAPFault $e) {
            print_r('Exception:'.$e);
        }
    }
    public function getUsername($userid){
        $res = $this->Data_model->get_single_data(array('name'=>'中骏森驰',
                                                            'type'=>'Baogong_6'),"WEIXININFOZJ");
        $url="https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=".$res['access_token']."&userid=".$userid;
            $json=self::http_request_json($url);//这个地方不能用file_get_contents
            $data=json_decode($json,true);
            return $data["name"];
    }
    public function getInfo(){
        $this->dbConnectsql = $this->load->database("oracle_mes",TRUE);
        $query = $this->dbConnectsql->query("Select T1.FNUMBER 产品代号,T1.FNAME 产品名称,TT.FQty 流转卡数量,t3.fsqty 已汇报量,T1.FDATE 日期,
			FTaskRTNumber 工艺路线 ,t2.foperno 工序号,t2.fopernumber 工序代码,t2.fopername 工站名称,t1.fbillno 派工单号,tt.fflowcardno 流转卡号
                        From zxec.m_flowcard tt 
                        left join zxec.M_Order t1 on tt.ftaskbillno=t1.fbillno 
                        left join zxec.m_orderrt t2 on t1.ftaskrtnumber=t2.frtnumber  AND T1.FBILLNO=T2.FBILLNO
                        left join zxec.m_orderrt t4 on t1.ftaskrtnumber=t4.frtnumber and t4.fopername='出炉检' AND T1.FBILLNO=T4.FBILLNO
                        LEFT JOIN  (select sum(fqty) FSQTY,ftaskoperno,flzk from  zxec.m_taskreport where flzk='过滤条件_流转卡' group by ftaskoperno,flzk) t3 on tt.fflowcardno=t3.flzk and t3.ftaskoperno=t2.foperno
                        Where not t1.fbillno is null  AND nvl(t1.FPQty,0)>0 AND nvl(tt.fstatus,0)=0 AND nvl(t1.fstatus,0)=0 
                        and (( not t4.fid is null and t2.foperno<t4.foperno)  or  t4.fid is null  ) and TT.FQty>nvl(t3.fsqty,0) and tt.fflowcardno = '1511137-4'
                        ORDER BY 流转卡号");
        $resDate = $query->result_array();
        //var_dump($resDate);
    }
    public function json(){
        header("content-type:text/html;charset=utf-8");
        try {
            $client = new SoapClient("http://192.168.0.41/BHService.asmx?wsdl");
            /*
            //一、完工汇报进入验证
            $parmas = array("strIP"       => "192.168.0.1",
                            "strModule"   => "MES",
                            "strUser"     => "123",
                            "strValue"    => "1511137-4");
            $return = $client->CheckSubmitBarCode($parmas);
            var_dump($return->CheckSubmitBarCodeResult->string);
             * 
             */
            $return = array("FTypeNumber" => "LZK",     //
                            "FTypeName"   => "流转卡",
                            "FValue"      => "1511421-2",
                            "FText"       => "1511421-2",);
            foreach ( $return as $key => $value ) { 
                $return[$key] = urlencode ( $value );  
            }
            $parmas = array("strIP"      => "192.168.0.1",
                            "strModule"  => "MES",
                            "strUser"    => "6522",
                            "strType"    => "WGHB",
                            "strEmp"     => "6522",
                            "strLZK"     => "1511421-2",
                            "strChkJson"      => "[".urldecode(json_encode($return))."]");
            $return = $client->GetTaskingByJson($parmas);
            $return = $return->GetTaskingByJSONResult;
            
            $str = str_replace("[[", "[", $return);
            $str = str_replace("\\", "/", $str);
            $arr = json_decode($str,true);
            $res = array("resDate"     => $arr,
                        "type"     => "WGHB");
            //$this->load->view("baogongFinish",$res);
        } catch (SOAPFault $e) {
            print_r('Exception:'.$e);
        }
    }
    public function getCodeType(){
        $value = trim($_GET['value']);
        $str = substr($value, 0,1);
        if($str === "E"){
            $strArr = $this->getCodeTypeByUserInfo($value);
        }else{
            $strArr = $this->getCodeTypeByMatterInfo($value);
        }
        echo json_encode($strArr);
    }
    public function getCodeTypeByMatterInfo($value){
        header("content-type:text/html;charset=utf-8");
        try {
            $client = new SoapClient("http://192.168.0.41/BHService.asmx?wsdl");
            
            //07.1.630820-P   物料代码
            //一、完工汇报进入验证
            $parmas = array("strIP"       => "192.168.0.159",
                            "strModule"   => "MES",
                            "strUser"     => "192.168.0.159",
                            "strValue"    => $value);
            $return = $client->GetMaterByJson($parmas);
            //var_dump($return);
            
            $resJson = json_decode($return->GetMaterByJsonResult,true);
            if(isset($resJson['IT_MATNR'][0]["FText"])){
                $res = 'true';
            }else{
                $res = 'false';
            }
            $arr = array("res" => $res,
                            "type"   => "matter",
                            "matterno" => $value);
            return $arr;
        } catch (SOAPFault $e) {
            print_r('Exception:'.$e);
        }
    }

    public function getCodeTypeByUserInfo($userno){
        header("content-type:text/html;charset=utf-8");
        try {
            $client = new SoapClient("http://192.168.0.41/BHService.asmx?wsdl");
            
            //1511137-4
            //一、完工汇报进入验证
            $parmas = array("strIP"       => "192.168.0.159",
                            "strModule"   => "MES",
                            "strUser"     => "192.168.0.159",
                            "strValue"    => $userno);
            $return = $client->CheckSubmitBarCode($parmas);
            //var_dump($return->CheckSubmitBarCodeResult->string);
            $return = $return->CheckSubmitBarCodeResult->string; 
            if($return['0'] === "成功"){
                $res = "true";
            }else{
                $res = "false";
            }
            $arr = array("res"         => $res,
                            "userno"   => $return['2'],
                            "username" => $return['3'],
                            "type"     => "user");
            return $arr;
        } catch (SOAPFault $e) {
            print_r('Exception:'.$e);
        }
    }
    public function back(){
        $UserNo = trim($_GET['UserNo']);
        $userid = substr($UserNo, 1);
        $this->index($userid);
    }
    /*
     * 产量查询
     */
    public function Login_MyOutput(){
        if (isset($_GET['code'])){
            $code = $_GET['code'];
            $res = $this->Data_model->get_single_data(array('name'=>'中骏森驰',
                                                            'type'=>'Baogong_6'),"WEIXININFOZJ");
            $this->get_userId_MyOutput($res['access_token'], $code,$res['agentid']);
        }else{
            echo "NO CODE";
        }
    }
    function get_userId_MyOutput($access_token,$code,$agentId){
        if($access_token){
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=".$access_token."&code=".$code."&agentid=".$agentId;
            $json=self::http_request_json($url);//这个地方不能用file_get_contents
            $data=json_decode($json,true);
            if(isset($data['errcode'])){
                if($data['errcode'] == "40001" or $data['errcode'] == '42001' or $data['errcode'] == '40014'){
                    $this->get_access_token();
                    
                    $newurl = "<?=base_url()?>/baogong/Login_MyOutput?code=".$code;
                    echo "<script language='javascript' type='text/javascript'>";  
                    echo "window.location.href='".$newurl."'";  
                    echo "</script>"; 
                    
                }else{
                    echo "页面请求错误，请检查！";
                }
            }else{
                $this->index_MyOutput($data['UserId']);
            }
        }else{
            $this->get_access_token();
            $this->Login_MyOutput();
        }
    }
    
    function index_MyOutput($userid){ 
        $username = $this->getUsername($userid);
        $startDate=date('Y-m-01 H:i:s', strtotime(date("Y-m-d")));
        $endDate = date('Y-m-d H:i:s', strtotime("$startDate +1 month -1 day"));
        $signPackage = array('username' => $username,
                                'workid' => $userid,
                                'status'   => 'show',
                                'startDate' => $startDate,
                                'endDate'   => $endDate);
        $this->load->view("MyOutput",$signPackage);
    }
    
    public function getOutputInfo(){
        $strIP     = '192.168.0.1';
        $strModule = 'MES';
        $strUser   = 'Admin';
        /*
        $startDate  = '2015-12-01 00:00:00';
        $endDate   = '2015-12-31 00:00:00';
        $strEmp    = '6522';
        */
        $username  = isset($_REQUEST['username'])?$_REQUEST['username']:"";
        $strEmp    = isset($_REQUEST['workid'])?$_REQUEST['workid']:'';
        $startDate  = isset($_REQUEST['startDate'])?$_REQUEST['startDate']:'';
        $endDate    = isset($_REQUEST['endDate'])?$_REQUEST['endDate']:'';
        
        $DateFrom = str_replace(" ", "T", $startDate);
        $DateTo   = str_replace(" ", "T", $endDate);
        header("content-type:text/html;charset=utf-8");
        try {
            $client = new SoapClient("http://192.168.0.41/BHService.asmx?wsdl");
            $parmas = array("strIP"      => $strIP,
                            "strModule"  => $strModule,
                            "strUser"    => $strUser,
                            "DateFrom"   => $DateFrom,
                            "DateTo"     => $DateTo,
                            "strEmp"     => $strEmp);
            $return = $client->Tasking001ByJSON($parmas);
            $return = $return->Tasking001ByJSONResult;
            
            $str = str_replace("[[", "[", $return);
            $str = str_replace("\\", "/", $str);
            $arr = json_decode($str,true);
            if(empty($arr)){
                $res = array('status' => 'faild');
            }else{
                $res = array('status' => 'success',
                                'resData' => $arr['M_Tasking001'],
                                'startDate' => $startDate,
                                'endDate'   => $endDate);
            }
            $this->load->view("MyOutput",$res);
        } catch (SOAPFault $e) {
            print_r('Exception:'.$e);
        }
    }
    
    public function backMyOutput(){
        $workid = isset($_REQUEST['workid'])?$_REQUEST['workid']:'';
        $this->Login_MyOutput($workid);
    }
    /*
     * 流转卡查询
     */
    public function LZKSearch(){
        $jsapiTicket = $this->getJsApiTicket();

        // 注意 URL 一定要动态获取，不能 hardcode.
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        
        $timestamp = time();
        $nonceStr = $this->createNonceStr();

        // 这里参数的顺序要按照 key 值 ASCII 码升序排序
        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";

        $signature = sha1($string);
        $signPackage = array(
            "appId"     => $this->appId,
            "nonceStr"  => $nonceStr,
            "timestamp" => $timestamp,
            "url"       => $url,
            "signature" => $signature,
            "rawString" => $string,
            "status"    => 'show'
        );
        $this->load->view("LZKSearch",$signPackage);
    }
    public function getLZKOutputInfo(){
        $LZKNO = isset($_REQUEST['LZKNO'])?$_REQUEST['LZKNO']:'';
        //$LZKNO = '15121302-40';
        $strIP = "192.168.0.41";
        $strModule = "MES";
        $strUser = "Admin";
        header("content-type:text/html;charset=utf-8");
        try {
            $client = new SoapClient("http://192.168.0.41/BHService.asmx?wsdl");
            $parmas = array("strIP"      => $strIP,
                            "strModule"  => $strModule,
                            "strUser"    => $strUser,
                            "strFLZK"    => $LZKNO);
            $return = $client->Tasking002ByJSON($parmas);
            $return = $return->Tasking002ByJSONResult;
            
            $str = str_replace("[[", "[", $return);
            $str = str_replace("\\", "/", $str);
            $arr = json_decode($str,true);
            if(empty($arr)){
                $res = array('status' => 'faild');
            }else{
                $res = array('status' => 'success',
                                'resData' => $arr['M_Tasking002']);
            }
            $this->load->view("LZKSearch",$res);
        } catch (SOAPFault $e) {
            print_r('Exception:'.$e);
        }
    }
    
    /*
     * 获取不良原因代码
     */
    public function getBadReasonByJson(){
        header("Access-Control-Allow-Origin:*");
        $strOperNumber = isset($_REQUEST['strOperNumber'])?$_REQUEST['strOperNumber']:'';
        //$strOperNumber = "S1001,S1003";
        
        $OperNumberArr = explode(',', $strOperNumber);
        $newstr = "";
        foreach ($OperNumberArr as $value) {
            $newstr = $newstr."'".$value."',";
        }
        $newstr=substr($newstr, 0,strlen($newstr)-1);
        header("content-type:text/html;charset=utf-8");
        try{
            $client = new SoapClient("http://192.168.0.41/BHService.asmx?wsdl");
            $parmas = array("strIP"      => "192.168.0.40",
                            "strModule"  => "MES",
                            "strUser"    => "Admin",
                            "strOperNumber"    => $newstr
                            //"strOperNumber"    => "'S1001','S1003'"
                );
            $return = $client->GetBadReasonByJSON($parmas);
            $return = $return->GetBadReasonByJSONResult;
            $arr = json_decode($return,true);
            echo json_encode($arr);
        }  catch (SOAPFault $e){
            print_r('Exception:'.$e);
        }
        
    }
    
}

